package com.guangyuan.network.mvc.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guangyuan.network.constants.ProductGroupConstant;
import com.guangyuan.network.dataobject.ProductGroup;
import com.guangyuan.network.external.OperatorFactory;

public class ProductGroupAction implements Action{
	
	public boolean updateProductGroup(HttpServletRequest request, HttpServletResponse response) {
		ProductGroup pg = getProductGroup(request, response);
		
		return OperatorFactory.getProductGroupOper().updateProductGroup(pg);
	}
	
	public boolean addProductGroup(HttpServletRequest request, HttpServletResponse response) {
		ProductGroup pg = getProductGroup(request, response);
		
		return OperatorFactory.getProductGroupOper().insertProductGroup(pg);
	}
	
	public boolean deleteProductGroup(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter(ProductGroupConstant.GROUP_ID);
		
		return OperatorFactory.getProductGroupOper().deleteProductGroup(id);
	}
	
	private ProductGroup getProductGroup(HttpServletRequest request, HttpServletResponse response) {
		ProductGroup pg = new ProductGroup();
		String idStr = request.getParameter(ProductGroupConstant.GROUP_ID);
		int id = Integer.parseInt((null != idStr && !"".equals(idStr) ? idStr : "0"));
		pg.setId(id);
		pg.setName(request.getParameter(ProductGroupConstant.GROUP_NAME));
		pg.setCnName(request.getParameter(ProductGroupConstant.GROUP_CNNAME));
		pg.setDescription(request.getParameter(ProductGroupConstant.GROUP_DESCRIPTION));
		//FIXME 修改操作者的名
		pg.setOperator("吴天翼");
		
		return pg;
	}
}
